package com.yusiji.config;

import com.yusiji.pojo.Configuration;
import com.yusiji.pojo.MapperStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author yusiji
 */
public class XMLMapperConfigBuilder {


    private Configuration configuration;
    public XMLMapperConfigBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    public void parseConfig(InputStream in) throws DocumentException {
        Document document = new SAXReader().read(in);
        Element rootElement = document.getRootElement();
        String nameSpace = rootElement.attributeValue("namespace");

//        List<Element> selectList = rootElement.selectNodes("//select");
//        List<Element> updateList = rootElement.selectNodes("//update");
//        List<Element> deleteList = rootElement.selectNodes("//delete");
//        List<Element> insertList = rootElement.selectNodes("//insert");
        List<Element> elements = rootElement.elements();

        for (Element element: elements) {
            String id =element.attributeValue("id");
            String resultType =element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql = element.getTextTrim();
            String sqlType = element.getQName().getName();
            MapperStatement mapperStatement = new MapperStatement();
            mapperStatement.setId(id);
            mapperStatement.setParamterType(paramterType);
            mapperStatement.setResultType(resultType);
            mapperStatement.setSql(sql);
            mapperStatement.setSqlType(sqlType);
            configuration.getMapperStatementMap().put(nameSpace+"."+id,mapperStatement);

        }




    }

}
